//
//  HMEmoticonPopView.swift
//  EmoticonKeyboard
//
//  Created by 周英培 on 16/6/2.
//  Copyright © 2016年 周英培. All rights reserved.
//

import UIKit

class HMEmoticonPopView: UIView {

    @IBOutlet weak var emoticonBtn: HMEmoticonButton!
    //加载xib
    class func loadPopView() -> HMEmoticonPopView {
    
        let pop = NSBundle.mainBundle().loadNibNamed("HMEmoticonPopView", owner: nil, options: nil).last as! HMEmoticonPopView
        
    
        return pop
    }
    
    func show(btn:HMEmoticonButton) {
        
        //将popview转换到window上面
        let window = UIApplication.sharedApplication().windows.last
        
        if self.superview == nil {
            
            window?.addSubview(self)
        }
        
        self.hidden = false
        
        //转换坐标系
        //convertRect(转换的对象的frame或者bounds，toView：转换到哪一个视图）
        //frame或者bounds 需要根据调用这个方法的对象是视图本身还是视图的父视图
        let rect = btn.superview!.convertRect(btn.frame, toView: window)
        
        //设置pop的frame
        center.x = CGRectGetMidX(rect)
        frame.origin.y = CGRectGetMaxY(rect) - bounds.height
        emoticonBtn.emoticon = btn.emoticon
    
    }
    
    //延迟执行
   func disMissAfter() {
        
        //延迟执行
        let time_t = dispatch_time(DISPATCH_TIME_NOW,Int64(Double(NSEC_PER_SEC)*2))
        dispatch_after(time_t, dispatch_get_main_queue()) {
            
//            self.removeFromSuperview()
            self.hidden = true
            
        }
        
    }
}
